package han.message;

import han.entity.MessageEntity;
import han.parser.MessageParser;

import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author: Han
 * @date: 2018/7/20 14:56
 * @dscription: 处理消息轮询接受到的消息
 */
public class MessageReceiver implements Runnable {

    /**
     * 线程安全的消息队列
     */
    private LinkedBlockingQueue<MessageEntity> messageQueue;
    private String msg;

    MessageReceiver(String msg, LinkedBlockingQueue<MessageEntity> messageQueue) {
        this.msg = msg;
        this.messageQueue = messageQueue;
    }

    @Override
    public void run() {
        //解析消息
        MessageEntity entity = MessageParser.message(msg);
        if (Objects.isNull(entity)) {
            return;
        }
        //放入群组消息到消息队列
        if (entity.getType().equals(MessageParser.GROUP_MESSAGE)) {
            messageQueue.add(entity);
        }
    }
}
